package com.Xht.www.dao;

import com.Xht.www.po.UserPointsPO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserPointsDAO extends BaseDAO {

    // 插入用户积分信息
    public boolean insertUserPoints(UserPointsPO userPoints) {
        String sql = "INSERT INTO user_points (user_id, total_points, consecutive_days, cumulative_days) VALUES (?,?,?,?)";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userPoints.getUserId());
            pstmt.setInt(2, userPoints.getTotalPoints());
            pstmt.setInt(3, userPoints.getConsecutiveDays());
            pstmt.setInt(4, userPoints.getCumulativeDays());
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(connection);
        }
    }

    // 根据积分ID查询用户积分信息
    public UserPointsPO getUserPointsById(int userId) {
        String sql = "SELECT * FROM user_points WHERE user_id = ?";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    UserPointsPO userPoints = new UserPointsPO();
                    userPoints.setPointsId(rs.getInt("points_id"));
                    userPoints.setUserId(rs.getInt("user_id"));
                    userPoints.setTotalPoints(rs.getInt("total_points"));
                    userPoints.setConsecutiveDays(rs.getInt("consecutive_days"));
                    userPoints.setCumulativeDays(rs.getInt("cumulative_days"));
                    return userPoints;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(connection);
        }
        return null;
    }

    // 更新用户积分信息
    public boolean updateUserPoints(UserPointsPO userPoints) {
        String sql = "UPDATE user_points SET total_points = ?, consecutive_days = ?, cumulative_days = ? WHERE user_id = ?";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userPoints.getTotalPoints());
            pstmt.setInt(2, userPoints.getConsecutiveDays());
            pstmt.setInt(3, userPoints.getCumulativeDays());
            pstmt.setInt(4, userPoints.getUserId());
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(connection);
        }
    }

    // 获取所有用户积分信息方法
    public List<UserPointsPO> getAllUserPoints() {
        List<UserPointsPO> pointsList = new ArrayList<>();
        String sql = "SELECT * FROM user_points";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                UserPointsPO userPoints = new UserPointsPO();
                userPoints.setPointsId(rs.getInt("points_id"));
                userPoints.setUserId(rs.getInt("user_id"));
                userPoints.setTotalPoints(rs.getInt("total_points"));
                userPoints.setConsecutiveDays(rs.getInt("consecutive_days"));
                userPoints.setCumulativeDays(rs.getInt("cumulative_days"));
                pointsList.add(userPoints);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(connection);
        }
        return pointsList;
    }
}